package com.hqyj.lyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqyj.lyf.mapper.ExamPaperMapper;
import com.hqyj.lyf.pojo.ExamPaper;

import com.hqyj.lyf.pojo.QuestionBank;
import com.hqyj.lyf.service.ExamPaperService;
import com.hqyj.lyf.service.ExamQuestionBankService;
import jdk.internal.org.objectweb.asm.Handle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Qiuluoer
 * @date 2022/5/28 13:37
 */
@Service
public class ExamPaperServiceImpl implements ExamPaperService {
    @Autowired
    private ExamQuestionBankService examQuestionBankService;
    @Autowired
    private ExamPaperMapper examPaperMapper;

    Map<String,Object> map;

    //生成试卷
    @Override
    public Map<String,Object> addExamPaper(ExamPaper examPaper, List<Integer> quIds) {
        map = new HashMap<>();
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        examPaper.setCreateTime(timestamp);
        //添加试卷
        int insert = examPaperMapper.insert(examPaper);
        //获取试卷id
        Map<String, Object> examPaperMap = queryByExamPaper(examPaper);
        List paperList =(List)examPaperMap.get("data");
        ExamPaper examPaper2 = (ExamPaper)paperList.get(0);
        Integer examPaperId = examPaper2.getExamPaperId();
        //添加试卷包含的题
        Boolean flag = examQuestionBankService.addExamQuestion(quIds, examPaperId);

        if (insert < 0 || flag == false){
            map.put("code",-1);
            map.put("msg","试卷生成失败");
            return map;
        }

        map.put("code",0);
        map.put("msg","试卷生成成功");
        return map;
    }

    //条件查询试卷
    @Override
    public Map<String,Object> queryByExamPaper(ExamPaper examPaper) {
            map= new HashMap<>();
            QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
            Integer examPaperId = examPaper.getExamPaperId();
            String examPaperName = examPaper.getExamPaperName();
            String  userName = examPaper.getUserName();
            Timestamp createTime = examPaper.getCreateTime();
            if(examPaperId != null && !(examPaperId.equals(0))){
                queryWrapper.eq("exam_paper_id",examPaperId);
                //System.out.println("exam_paper_id------------");
            }
            if (examPaperName != null && !examPaperName.equals("")){
                queryWrapper.like("exam_paper_name",examPaperName);
                //System.out.println("exam_paper_name------------");
            }
            if (userName != null && !userName.equals("")){
                queryWrapper.eq("username",userName);
                //System.out.println("subId------------");
            }
            if (createTime != null){
                queryWrapper.eq("create_time",createTime);
                //System.out.println("create_time------------");
            }
        List<ExamPaper>  list = examPaperMapper.selectList(queryWrapper);
        map.put("code",0);
        map.put("msg","试卷信息");
        map.put("data",list);

        return map;

    }

    //查询所有试卷
    @Override
    public Map<String,Object> queryAllExamPaper() {
        Map<String, Object> map = queryByExamPaper(new ExamPaper());
        return map;
    }

    @Override
    public Map<String, Object> deleteExamPaper(List<Integer> examIds) {
        map = new HashMap<>();
        if (examIds .isEmpty()&&examIds.size()==0) {
            map.put("code",-1);
            map.put("msg","删除失败");
            return map;
        }
        QueryWrapper<ExamPaper> wrapper = new QueryWrapper<>();
        wrapper.in("exam_paper_id",examIds);
        examPaperMapper.delete(wrapper);
        map.put("code",0);
        map.put("msg","删除成功");
        return map;
    }
}
